
<%= javascript_tag "window._token = '#{form_authenticity_token}'" if ActionController::Base.allow_forgery_protection %>
<%= javascript_include_tag "prototype", "effects", "dragdrop", "controls" %>
 <%# javascript_include_tag :fckeditor %>  
   <script src="/validation/lib/prototype.js" type="text/javascript"></script>
  <script src="/validation/lib/effects.js" type="text/javascript"></script>
  <script src="/validation/validation_cn.js" type="text/javascript"></script>
 <script>
 function MyClass()
{
        this.UpdateEditorFormValue = function()
        {
                for ( i = 0; i < parent.frames.length; ++i )
                        if ( parent.frames[i].FCK )
                                parent.frames[i].FCK.UpdateLinkedField();
        }
}
// instantiate the class
var MyObject = new MyClass();
</script>
<p class="smallNav">
  <%= link_to "专业量表管理", :action => "index"  %>>>
  <%= link_to "编辑量表", :action => "edit", :id => @part.survey  %>>>
  添加题目
</p>
<br/>
<div>
  <div id="questionsDiv">
    <!--题目列表--->
    <%= render :partial => "questions", :object => @part %>
  </div>
  <!--a href="javascript:;" onclick="Element.show('add_form_div');" style="margin-left:0px">添加题目</a-->
  <!-- quesiton添加表单-->
  <div id="add_form_div" style=" margin-left:0px"><br/>
    <form id="form0" action="/surveys/prof_surveys/create_question" >
      <input type="hidden" value="<%=@part.id%>" name="part_id"/>
      清输入题目：
       <input type="button" value="保存" onclick="saveQuestion($('form0'));" class="cButton" style="margin-left: 50px"/>
      <br /><div></div> 
      <%#fckeditor_textarea(:question, :description, :toolbarSet=>"Easy" , :width => '500px', :height => '240px') %>
      <textarea cols="50" rows="2" name="question[description]"></textarea>
      <br/><br /><!--class="required validate-integer"-->
      <div id="message" style="display:none"> </div>
      题号：<input type="text"  name="question[number]" size="8"><br />
      <a href="javascript:;" onclick="addChoice($('form0'), 'choice0');">添加选项</a><br />
       选项<input type="text" name="choice.content[]" value=""/>&nbsp;
       分值<input type="text" name="choice.value[]"/><br />
       选项<input type="text" name="choice.content[]" value=""/>&nbsp;
       分值<input type="text" name="choice.value[]"/><br/>
    <!--input type="submit" value="保存" class="cButton" style="margin-left: 50px"/-->
    </form>
  </div>
  <!-- quesiton添加表单结束 -->

</div>
<div id="back" style="margin-left: 20px;margin-top: 20px ;"><font size="5">
<%= link_to "完成", :action => "index" %></font>
</div>

<script type="text/javascript">
//变量声明
var question_id = 0;
var choice_num = 2;

//给题目添加一个选项
function addChoice(parent, id) {
  parent.appendChild(createChoice(id, true));
}

//创建一个选项
function createChoice(id, isRemove) {
  var choiceDiv = document.createElement("div");
  var a = createA("javascript:;", "<font color=red>删除</font>")
  a.onclick = function(){ removeElement(choiceDiv) }

  choiceDiv.id = id;
  choiceDiv.appendChild(createLabel("选项"));
  choiceDiv.appendChild(createTextField("choice.content[]"));

  choiceDiv.appendChild(createLabel("&nbsp;&nbsp;"));

  choiceDiv.appendChild(createLabel("分值"));
  choiceDiv.appendChild(createTextField("choice.value[]"));

  if(isRemove){
    choiceDiv.appendChild(a);
  }

  choice_num++;
  return choiceDiv;
}

//删除自身
function removeElement(el){
  el.parentNode.removeChild(el);
}

//创建textfield
function createTextField(name) {
  var newInput = document.createElement("input");
  newInput.type = "text";
  newInput.name = name;
  return newInput;
}

//创建换行符
function br(){
  return document.createElement("br");
}

//创建文本
function createLabel(content){
  var label = document.createElement("span");
  label.innerHTML = content;
  return label;
}

//创建链接
function createA(href, content) {
  var a = document.createElement("a");
  a.href = href;
  a.innerHTML = content;
  return a;
}

//创建select
function createSelect(name){
  var select = document.createElement("select");
  select.name = name;
  select.appendChild(createOption("<%=TEST_TYPE_TREND%>", "<%=TEST_TYPE_TREND%>"));
  select.appendChild(createOption("<%=TEST_TYPE_JUMP%>", "<%=TEST_TYPE_JUMP%>"));
  select.appendChild(createOption("<%=TEST_TYPE_COUNT%>", "<%=TEST_TYPE_COUNT%>"));
}

//创建option
function createOption(value, content){
  var option = document.createElement("option");
  option.value = value;
  option.innerHTML = content;
  return option;
}

//创建按钮
function　createInput(value, type) {
  var input = document.createElement("input");
  input.value = value;
  input.type = type;
  return input;
}

//异步保存题目
function saveQuestion(form) {
/*alert($("question__description_editor").value);*/
/*alert(MyObject);*/
MyObject.UpdateEditorFormValue();
    new Ajax.Updater(
      "questionsDiv",
      form.action,
      {
        method: "post",
        asynchronous: true,
        evalScripts: true,
        onFailure: reportError,
        onComplete: gogogo,
        parameters: Form.serialize(form) + '&authenticity_token=' + window._token
      }
    );
}

function gogogo() {
 // alert("OK");
  }
function reportError(){
   // alert("Failure");
//$("message").show() ;
//alert("题目序号不能重复！");
 }
//删除所有子节点
function removeAllNodes(parentNode){
  while (parentNode.firstChild) {
    var oldNode = parentNode.removeChild(parentNode.firstChild);
    oldNode = null;
  }
}

window.onload = function() {

}
</script>
